package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.*;
import com.sxkf.template.service.PostCollectService;
import com.sxkf.template.service.PostService;
import com.sxkf.template.service.TopicService;
import com.sxkf.template.service.UserService;
import com.sxkf.template.vo.request.CollectDeleteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 帖子收藏 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-17 15:24:12
 */
@RestController
@RequestMapping("/postCollect")
@Api(tags = "帖子收藏")
public class PostCollectController {

    @Resource
    private PostCollectService postCollectService;

    @Resource
    private PostService postService;
    @Resource
    private TopicService topicService;
    @Resource
    private UserService userService;


    @ApiOperation(value = "收藏")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody PostCollect postCollect){
        Boolean res = postCollectService.add(postCollect);
        return R.success(res);
    }

    @ApiOperation(value = "取消收藏")
    @GetMapping(value = "/del")
    public R<?> del(@RequestParam String id){
        postCollectService.removeById(id);
        return R.success();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/deleteBatch")
    public R<?> deleteBatch(CollectDeleteVO collectDeleteVO){
        postCollectService.remove(new LambdaQueryWrapper<PostCollect>()
                .eq(PostCollect::getUserId,collectDeleteVO.getUserId())
                .in(PostCollect::getPostId,collectDeleteVO.getTargetIds()));
        return R.success();
    }


    @ApiOperation(value = "是否收藏")
    @GetMapping(value = "/isAdd")
    public R<?> isAdd(@RequestParam String userId,
                      @RequestParam String postId){
        Boolean res = postCollectService.isAdd(userId, postId);
        return R.success(res);
    }


    @ApiOperation(value = "分页查收藏列表")
    @GetMapping(value = "/page")
    public R<IPage<Post>> page(@RequestParam Integer pageNo,
                               @RequestParam Integer pageSize,
                               @RequestParam String userId,
                               @ApiParam("帖子类型  1=互助赚，2=动态")
                               @RequestParam String type){

        Page<Post> page = postCollectService.pageByType(new Page<>(pageNo,pageSize),userId,type);
        List<Post> postList = page.getRecords();
        if (postList.isEmpty()){
            return R.success(page);
        }
        List<String> topicIdList = postList.stream().map(Post::getTopicId).collect(Collectors.toList());

//        Page<PostCollect> page = postCollectService.page(new Page<>(pageNo,pageSize),new LambdaQueryWrapper<PostCollect>()
//                .eq(PostCollect::getUserId,userId));

//        Page<Post> postPage = new Page<>(pageNo, pageSize);
//        if (page.getTotal() == 0){
//            return R.success(postPage);
//        }
//        List<String> postIdList = page.getRecords().stream().map(PostCollect::getPostId).collect(Collectors.toList());
//        List<Post> postList = postService.listByIds(postIdList);
//
//        List<String> topicIdList = postList.stream().map(Post::getTopicId).collect(Collectors.toList());


        List<Topic> topicList = topicService.listByIds(topicIdList);
        Map<String, String> topicTitleMap = topicList.stream().collect(Collectors.toMap(Topic::getId, Topic::getTitle));

        List<String> userIdList = postList.stream().map(Post::getUserId).collect(Collectors.toList());
        List<User> userList = userService.listByIds(userIdList);
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));

        postList.stream().forEach(post -> {
            User user = userMap.get(post.getUserId());
            post.setTopicTitle(topicTitleMap.get(post.getTopicId()));
            post.setAvatar(user.getAvatar());
            post.setNickName(user.getNickName());
        });

//        postPage.setRecords(postList);
//        return R.success(postPage);
        page.setRecords(postList);
        return R.success(page);

    }


}
